{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "94007b14-bee9-4ae8-84d0-3df8a84c5d83",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "# 国内代理方式\n",
    "client = OpenAI(\n",
    "    api_key = \"sk-y7DHfp9fzuCxOVm2158638099f9541D3833aB4F4Ed674aCf\",\n",
    "    base_url = \"https://vip.apiyi.com/v1\"    # 此处代理方式，如果是OpenAI官方接口需调整接口地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "16cec562-ba31-45fb-969e-39f7ed35a954",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['babbage-002', 'chatgpt-4o-latest', 'claude-3-5-haiku-20241022', 'claude-3-5-sonnet-20240620', 'claude-3-5-sonnet-20241022', 'claude-3-7-sonnet-20250219', 'claude-3-7-sonnet-20250219-thinking', 'claude-3-haiku-20240307', 'claude-3-opus-20240229', 'claude-3-sonnet-20240229', 'cld-3-5-sonnet-20240620', 'cld-3-5-sonnet-20241022', 'cld-3-7-sonnet-20250219', 'cld-3-7-sonnet-20250219-thinking', 'code-davinci-edit-001', 'dall-e-2', 'dall-e-3', 'davinci-002', 'deepseek-chat', 'deepseek-r1', 'deepseek-reasoner', 'deepseek-v3', 'gemini-1.5-flash', 'gemini-1.5-flash-002', 'gemini-1.5-flash-8b', 'gemini-1.5-flash-latest', 'gemini-1.5-pro', 'gemini-1.5-pro-001', 'gemini-1.5-pro-002', 'gemini-1.5-pro-latest', 'gemini-2.0-flash', 'gemini-2.0-flash-exp', 'gemini-2.0-flash-exp-image-generation', 'gemini-2.0-flash-thinking-exp', 'gemini-2.0-flash-thinking-exp-01-21', 'gemini-2.0-flash-thinking-exp-1219', 'gemini-2.0-pro-exp-02-05', 'gemini-exp-1114', 'gemini-exp-1121', 'gemini-exp-1206', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0125', 'gpt-3.5-turbo-0301', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-1106', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-16k-0613', 'gpt-3.5-turbo-instruct', 'gpt-4', 'gpt-4-0125-preview', 'gpt-4-0314', 'gpt-4-0613', 'gpt-4-1106-preview', 'gpt-4-32k', 'gpt-4-32k-0314', 'gpt-4-32k-0613', 'gpt-4-all', 'gpt-4-gizmo-*', 'gpt-4-turbo', 'gpt-4-turbo-2024-04-09', 'gpt-4-turbo-preview', 'gpt-4-vision-preview', 'gpt-4.5-preview', 'gpt-4.5-preview-2025-02-27', 'gpt-4o', 'gpt-4o-2024-05-13', 'gpt-4o-2024-08-06', 'gpt-4o-2024-11-20', 'gpt-4o-all', 'gpt-4o-audio-preview', 'gpt-4o-audio-preview-2024-10-01', 'gpt-4o-gizmo-*', 'gpt-4o-lite', 'gpt-4o-mini', 'gpt-4o-mini-2024-07-18', 'gpt-4o-mini-audio-preview', 'gpt-4o-mini-audio-preview-2024-12-17', 'gpt-4o-mini-realtime-preview-2024-12-17', 'gpt-4o-mini-search-preview', 'gpt-4o-mini-search-preview-2025-03-11', 'gpt-4o-realtime-preview', 'gpt-4o-realtime-preview-2024-10-01', 'gpt-4o-realtime-preview-2024-12-17', 'gpt-4o-search-preview', 'gpt-4o-search-preview-2025-03-11', 'grok-2', 'grok-2-1212', 'grok-2-imageGen', 'grok-2-search', 'grok-2-vision-1212', 'grok-3', 'grok-3-deepsearch', 'grok-3-imageGen', 'grok-3-reasoning', 'grok-3-search', 'grok-beta', 'grok-vision-beta', 'ministral-3b-latest', 'ministral-8b-latest', 'mistral-embed', 'mistral-large-2502-15-1-rc2', 'mistral-large-latest', 'mistral-large-pixtral-2411', 'mistral-moderation-latest', 'mistral-small', 'mistral-small-2501', 'mistral-small-latest', 'mj_fast_blend', 'mj_fast_describe', 'mj_fast_high_variation', 'mj_fast_imagine', 'mj_fast_low_variation', 'mj_fast_modal', 'mj_fast_pan', 'mj_fast_reroll', 'mj_fast_shorten', 'mj_fast_upload', 'mj_fast_upscale', 'mj_fast_upscale_creative', 'mj_fast_upscale_subtle', 'mj_fast_variation', 'mj_fast_zoom', 'mj_relax_blend', 'mj_relax_describe', 'mj_relax_high_variation', 'mj_relax_imagine', 'mj_relax_low_variation', 'mj_relax_modal', 'mj_relax_pan', 'mj_relax_reroll', 'mj_relax_shorten', 'mj_relax_upload', 'mj_relax_upscale', 'mj_relax_upscale_creative', 'mj_relax_upscale_subtle', 'mj_relax_variation', 'mj_relax_zoom', 'mj_turbo_blend', 'mj_turbo_describe', 'mj_turbo_high_variation', 'mj_turbo_imagine', 'mj_turbo_low_variation', 'mj_turbo_modal', 'mj_turbo_pan', 'mj_turbo_reroll', 'mj_turbo_shorten', 'mj_turbo_upload', 'mj_turbo_upscale', 'mj_turbo_upscale_creative', 'mj_turbo_upscale_subtle', 'mj_turbo_variation', 'mj_turbo_zoom', 'moonshot-v1-128k', 'moonshot-v1-128k-vision-preview', 'moonshot-v1-32k', 'moonshot-v1-32k-vision-preview', 'moonshot-v1-8k', 'moonshot-v1-8k-vision-preview', 'moonshot-v1-auto', 'o1', 'o1-2024-12-17', 'o1-mini', 'o1-mini-2024-09-12', 'o1-preview', 'o1-preview-2024-09-12', 'o3-mini', 'o3-mini-2025-01-31', 'omni-moderation-2024-09-26', 'omni-moderation-latest', 'pixtral-12b', 'pixtral-12b-latest', 'pixtral-large-2411', 'pixtral-large-latest', 'qwq-32b', 'text-ada-001', 'text-babbage-001', 'text-curie-001', 'text-davinci-002', 'text-davinci-003', 'text-davinci-edit-001', 'text-embedding-3-large', 'text-embedding-3-small', 'text-embedding-ada-002', 'text-moderation-007', 'text-moderation-latest', 'text-moderation-stable', 'tts-1', 'tts-1-1106', 'tts-1-hd', 'tts-1-hd-1106', 'whisper-1']\n"
     ]
    }
   ],
   "source": [
    "def getOpenAiModelList():\n",
    "    # 获取当前模型列表\n",
    "    models = client.models.list()\n",
    "    # models.data[0].id\n",
    "    model_list = [model.id for model in models.data]\n",
    "    return model_list\n",
    "print(getOpenAiModelList())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eae5ad3b-b3da-4d8a-a9cd-625b12b1f227",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model(id='gpt-4o-mini', created=1626777600, object='model', owned_by='openai', permission=[{'id': 'modelperm-LwHkVFn8AcMItP432fKKDIKJ', 'object': 'model_permission', 'created': 1626777600, 'allow_create_engine': True, 'allow_sampling': True, 'allow_logprobs': True, 'allow_search_indices': False, 'allow_view': True, 'allow_fine_tuning': False, 'organization': '*', 'group': None, 'is_blocking': False}], root='gpt-4o-mini', parent=None)\n"
     ]
    }
   ],
   "source": [
    "def getModelDetail(model):\n",
    "    model = client.models.retrieve(model)\n",
    "    return model\n",
    "print(getModelDetail(\"gpt-4o-mini\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aadd0495-dfae-4718-aa95-5339e4220a09",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "【回复内容】：\n",
      " ```python\n",
      "def quick_sort(arr):\n",
      "    if len(arr) <= 1:\n",
      "        return arr\n",
      "    pivot = arr[len(arr) // 2]\n",
      "    left = [x for x in arr if x < pivot]\n",
      "    middle = [x for x in arr if x == pivot]\n",
      "    right = [x for x in arr if x > pivot]\n",
      "    return quick_sort(left) + middle + quick_sort(right)\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "def getChatCompletionsContext(messages, model = \"gpt-4o-mini\"):\n",
    "    data= client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        max_tokens=1000,\n",
    "        temperature=0\n",
    "    )\n",
    "    return data.choices[0].message\n",
    "    \n",
    "messages=[\n",
    "            {\"role\": \"developer\", \"content\": \"你是一位python专家，你需要按照客户的要求，帮助客户完成工作；输出的每一步都要有逻辑顺序，step by step.\"},\n",
    "            {\"role\": \"user\", \"content\": \"生成可执行的快速排序 Python 代码，只输出代码块内容，函数名quick_sort\"}\n",
    "        ]\n",
    "message = getChatCompletionsContext(messages)\n",
    "print(\"【回复内容】：\\n\", message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "94491e1c-a46a-444c-8a1a-52d4b4449432",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 9, 32, 36, 37, 38, 52, 55, 72, 109]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def quick_sort(arr):\n",
    "    if len(arr) <= 1:\n",
    "        return arr\n",
    "    pivot = arr[len(arr) // 2]\n",
    "    left = [x for x in arr if x < pivot]\n",
    "    middle = [x for x in arr if x == pivot]\n",
    "    right = [x for x in arr if x > pivot]\n",
    "    return quick_sort(left) + middle + quick_sort(right)\n",
    "quick_sort([38,55,2,9,109,32,37,52,72,1,36])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "865769bc-6f2d-4c8a-a193-fb5ac9ab045d",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChoiceDelta(content='', function_call=None, refusal=None, role='assistant', tool_calls=None)\n",
      "ChoiceDelta(content='1', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='.', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' **', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='冷', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='笑', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='话', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='一', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='**', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='：\\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  ', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' 两', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='个', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='程序', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='员', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='在', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='路', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='上', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='遇', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='到', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='一个', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='陷', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='阱', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='，', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='A', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='说', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='：“', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='我们', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='用', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='代码', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='迭', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='代', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='一下', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='！”', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' B', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='回答', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='：“', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='可', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='惜', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='没有', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='调', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='试', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='工具', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='。”\\n\\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='2', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='.', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' **', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='冷', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='笑', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='话', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='二', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='**', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='：\\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  ', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' 为什么', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='程序', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='员', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='总', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='搞', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='不', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='清', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='楚', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='“', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='周', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='一', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='”', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='与', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='“', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='周', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='五', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='”的', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='区别', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='？', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='因为', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='他们', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='总', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是在', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='数组', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='的', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='边', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='界', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='条件', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='上', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='纠', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='结', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='！\\n\\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='3', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='.', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' **', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='今天', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='几', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='号', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='？', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='**', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  \\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  ', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' 今天', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='202', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='3', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='年', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='10', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='月', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='9', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='日', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='。\\n\\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='4', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='.', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' **', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='明', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='天', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='周', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='几', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='？', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='**', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  \\n', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='  ', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=' 明', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='天', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='是', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='周', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='二', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content='。', function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "ChoiceDelta(content=None, function_call=None, refusal=None, role=None, tool_calls=None)\n",
      "1. **冷笑话一**：\n",
      "   两个程序员在路上遇到一个陷阱，A说：“我们用代码迭代一下！” B回答：“可惜没有调试工具。”\n",
      "\n",
      "2. **冷笑话二**：\n",
      "   为什么程序员总是搞不清楚“周一”与“周五”的区别？因为他们总是在数组的边界条件上纠结！\n",
      "\n",
      "3. **今天是几号？**  \n",
      "   今天是2023年10月9日。\n",
      "\n",
      "4. **明天是周几？**  \n",
      "   明天是周二。\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# 流式返回\n",
    "def getChatCompletionsStream(messages, model = \"gpt-4o-mini\"):\n",
    "    response = client.chat.completions.create(\n",
    "        model=model,\n",
    "        messages=messages,\n",
    "        stream=True,           # 流式传输，直至data：[DONE]，默认为False\n",
    "        max_tokens=1000,\n",
    "        temperature=0\n",
    "    )\n",
    "    return response\n",
    "    \n",
    "# 将回复内容添加至消息中，作为下次调用的上下文\n",
    "messages.append({\"role\": message.role, \"content\": message.content})\n",
    "# 新一轮的对话\n",
    "messages.append({\"role\": \"user\", \"content\": \"1. 讲两个程序员的冷笑话；2.今天是几号？3.明天是周几？\"})\n",
    "\n",
    "# 流式获取新的消息\n",
    "response = getChatCompletionsStream(messages)\n",
    "content = []\n",
    "role = None\n",
    "for chunk in response:\n",
    "    # print(chunk.choices[0].delta)\n",
    "    if hasattr(chunk.choices[0].delta, \"role\"):\n",
    "        role = getattr(chunk.choices[0].delta, \"role\")\n",
    "        # print(role)\n",
    "    if chunk.choices[0].delta.content is not None:\n",
    "        # print(chunk.choices[0].delta.content)\n",
    "        content.append(chunk.choices[0].delta.content)\n",
    "print(''.join(content))\n",
    "print(role)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "510d8d89-4a9f-4144-9724-fd1b70d9d110",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'role': 'developer',\n",
       "  'content': '你是一位python专家，你需要按照客户的要求，帮助客户完成工作；输出的每一步都要有逻辑顺序，step by step.'},\n",
       " {'role': 'user', 'content': '生成可执行的快速排序 Python 代码，只输出代码块内容，函数名quick_sort'},\n",
       " {'role': 'assistant',\n",
       "  'content': '```python\\ndef quick_sort(arr):\\n    if len(arr) <= 1:\\n        return arr\\n    pivot = arr[len(arr) // 2]\\n    left = [x for x in arr if x < pivot]\\n    middle = [x for x in arr if x == pivot]\\n    right = [x for x in arr if x > pivot]\\n    return quick_sort(left) + middle + quick_sort(right)\\n```'},\n",
       " {'role': 'user', 'content': '1. 讲两个程序员的冷笑话；2.今天是几号？3.明天是周几？'},\n",
       " {'role': None,\n",
       "  'content': '1. **冷笑话一**：\\n   两个程序员在路上遇到一个陷阱，A说：“我们用代码迭代一下！” B回答：“可惜没有调试工具。”\\n\\n2. **冷笑话二**：\\n   为什么程序员总是搞不清楚“周一”与“周五”的区别？因为他们总是在数组的边界条件上纠结！\\n\\n3. **今天是几号？**  \\n   今天是2023年10月9日。\\n\\n4. **明天是周几？**  \\n   明天是周二。'}]"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "messages.append({\"role\": role , \"content\": ''.join(content)})\n",
    "messages"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langChain",
   "language": "python",
   "name": "langchain"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
